'''
Created on Mar 21, 2010

modified heavily by RJ based originally upon

http://www.pygame.org/docs/tut/intro/intro.html

Just an example showing how to load and draw a moving image with pygame

@author: Administrator
'''

import sys, pygame, os, random
from pygame.locals import *
from idlelib.AutoCompleteWindow import KEYPRESS_SEQUENCES

#screen size
size = width, height = 1200, 800
zombies = []
#just need to figure out how to get classes from other files
#BasicZombie inherits from pygame's sprite class
class BasicZombie(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initalizer
        self.image = pygame.image.load("../Images/TestZombie2.gif")
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(self.rect.right, width - self.rect.right), random.randint(self.rect.bottom, height - self.rect.bottom))
        self.x_velocity = random.randint(1, 8)
        self.y_velocity = random.randint(1, 8)
        self.x_acceleration = 0
        self.y_acceleration = 0
        self.x_Previous = self.rect.centerx
        self.y_Previous = self.rect.centery
        
    def getRect(self):
        return self.rect
    
    def setRect(self, i):
        self.rect = i
    
    #gotta create a property to use accessors/mutators
    theRect = property(getRect, setRect)
    
    def update(self):
        self.rect.move_ip((self.x_velocity, self.y_velocity))
        collisionIndex = self.rect.collidelist(zombies)
        if collisionIndex != -1 and zombies[collisionIndex] != self:
            self.rect.center = (self.x_Previous, self.y_Previous)
            self.x_velocity = -self.x_velocity
            self.y_velocity = -self.y_velocity
            
        if self.rect.left < 0 or self.rect.right > width:
            self.x_velocity = -self.x_velocity
            self.rect.center = (self.x_Previous, self.rect.centery)
                  
        if self.rect.top < 0 or self.rect.bottom > height:
            self.y_velocity = -self.y_velocity
            self.rect.center = (self.rect.centerx, self.y_Previous)
        
        self.x_Previous = self.rect.centerx
        self.y_Previous = self.rect.centery
#class ended, making  instance of the class

zombies = [BasicZombie(), BasicZombie(), BasicZombie(), BasicZombie(), BasicZombie()]
zombies_sprites = [pygame.sprite.RenderClear(zombies[0]), pygame.sprite.RenderClear(zombies[1]), pygame.sprite.RenderClear(zombies[2]), pygame.sprite.RenderClear(zombies[3]), pygame.sprite.RenderClear(zombies[4])]

def load_BGMusic(soundFile):
    music = pygame.mixer.Sound(soundFile)
    return music

if __name__ == '__main__':
    random.seed()
    pygame.init()
    black = 0, 0, 0
    screen = pygame.display.set_mode(size)
    fullScreen = 0
    lastKey = -1
    bgMusic = load_BGMusic("../BGMusic/skinonourteeth.wav")
    bgMusic.play()
    while 1:
        pygame.time.delay(10)  ## replace with time-based (timeElapsed method later)
        
        screen.fill(black)
        for object in zombies_sprites:
            object.update()
            object.draw(screen)

        pygame.display.flip()
        
        #pygame events, even tracks keyboard input easily, very nice
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_RETURN and lastKey != K_RETURN:
                    lastKey = K_RETURN
                    if fullScreen == 0:
                        screen = pygame.display.set_mode(size, FULLSCREEN)
                        fullScreen = 1
                    else:
                        screen = pygame.display.set_mode(size)
                        fullScreen = 0
                elif event.key == K_SPACE and lastKey != K_SPACE:
                    lastKey = K_SPACE
                    zombies.append(BasicZombie())
                    zombies.append(BasicZombie())
                    zombies_sprites.append(pygame.sprite.RenderClear(zombies[-2]))
                    zombies_sprites.append(pygame.sprite.RenderClear(zombies[-1]))
                else:
                    lastKey = -1
            else:
                lastKey = -1
            
            